package com.simple.gateway.pay.strategy;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import lombok.Data;
import lombok.SneakyThrows;

import java.util.Map;

/**
 * @description: 請求簽名策略
 * @author: Mr.Kang
 * @create: 2021-04-10 11:49
 **/
@Data
public class BodySignStrategy extends AbstractSignStrategy<Map<String, Object>> {

    private String uri;
    private String mid;
    private String ts;
    private String version;

    private ObjectMapper ob = new ObjectMapper();

    @Override
    void getUri(String uri) {
        this.uri = uri;
    }

    @Override
    void getMid(String mid) {
        this.mid = mid;
    }

    @Override
    void getTs(String ts) {
        this.ts = ts;
    }

    @Override
    void getVersion(String version) {
        this.version = version;
    }

    @SneakyThrows
    @Override
    Map<String, Object> convertRequest(String body) {
        Map<String, Object> map = ob.readValue(body, Map.class);

        Map<String, Object> signMap = Maps.newHashMap();
        signMap.put("version", this.version);
        signMap.put("mid", this.mid);
        signMap.put("ts", this.ts);
        signMap.put("orderNo", map.get("orderNo"));
        if (this.uri.contains("directPay")) {
            signMap.put("amount", map.get("amount"));
            signMap.put("currency", map.get("currency"));
            signMap.put("firstName", map.get("firstName"));
            signMap.put("lastName", map.get("lastName"));
            signMap.put("cardNo", map.get("cardNo"));
            signMap.put("expireYear", map.get("expireYear"));
            signMap.put("expireMonth", map.get("expireMonth"));
            signMap.put("cvv", map.get("cvv"));
            signMap.put("asyncUrl", map.get("asyncUrl"));
        } else if (this.uri.contains("cashier")) {
            signMap.put("amount", map.get("amount"));
            signMap.put("currency", map.get("currency"));
            signMap.put("asyncUrl", map.get("asyncUrl"));
            signMap.put("redirectUrl", map.get("redirectUrl"));
        } else if (this.uri.contains("trade/refund")) {
            signMap.put("refundAmount", map.get("refundAmount"));
            signMap.put("refundOrderNo", map.get("refundOrderNo"));
            signMap.put("refundCurrency", map.get("refundCurrency"));
            signMap.put("refundReason", map.get("refundReason"));
        }
        else if (uri.contains("transfer/query")) {

        }
        else if (uri.contains("transfer")) {
            signMap.put("transferType", map.get("transferType"));
            signMap.put("transferAmount", map.get("transferAmount"));
            signMap.put("payeeAccount", map.get("payeeAccount"));
            signMap.put("payeeName", map.get("payeeName"));
            signMap.put("bankName", map.get("bankName"));
            signMap.put("subbranch", map.get("subbranch"));
        }
        return signMap;
    }


    private final Gson gson = new Gson();

    @SneakyThrows
    @Override
    Map<String, Object> converResponse(Map<String, Object> map, Long ts) {
        Map<String, Object> signMap = Maps.newHashMap();
        signMap.put("ts", ts);
        signMap.put("mid", this.mid);
        signMap.put("version", this.version);
        signMap.put("orderNo", map.get("orderNo"));
        if (this.uri.contains("directPay")
                || this.uri.contains("redirectPay")
                || this.uri.contains("/v3/query")
                || this.uri.contains("trade/query")
        ) {
            signMap.put("serialNo", map.get("serialNo") + "");
            signMap.put("currency", map.get("currency"));
            signMap.put("amount", map.get("amount"));
            signMap.put("payStatus", map.get("payStatus"));
        } else if (this.uri.contains("trade/refund")) {
            signMap.put("refundOrderNo", map.get("refundOrderNo"));
            signMap.put("refundNo", map.get("refundNo"));
            signMap.put("refundAmount", map.get("refundAmount"));
            signMap.put("refundCurrency", map.get("refundCurrency"));
            signMap.put("refundStatus", map.get("refundStatus"));
        }
        else if (uri.contains("cashier")) {
            signMap.put("serialNo", map.get("serialNo") + "");
            signMap.put("currency", map.get("currency"));
            signMap.put("amount", map.get("amount"));
            signMap.put("payStatus", map.get("payStatus"));
        }
        else if (uri.contains("transfer/query")) {
            signMap.put("serialNo", map.get("serialNo") + "");
            signMap.put("transferStatus", map.get("transferStatus"));
            signMap.put("transferAmount", map.get("transferAmount"));
        }
        else if (uri.contains("transfer")) {
            signMap.put("serialNo", map.get("serialNo") + "");
            signMap.put("transferStatus", map.get("transferStatus"));
            signMap.put("transferAmount", map.get("transferAmount"));
        }
        return signMap;
    }


}
